//
//  LetterTabelCell.m
//  Growth
//
//  Created by  on 12-2-9.
//  Copyright (c) 2012年 MEETStudio. All rights reserved.
//

#import "LetterTabelCell.h"
#import <QuartzCore/QuartzCore.h>
#import "Config.h"

@implementation LetterTabelCell


@synthesize userId;
@synthesize nameLabel;
@synthesize iconButton;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void)loadCellWith:(NSMutableDictionary *)dict
{
	
	
	UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.tag = 1;
    label.lineBreakMode = UILineBreakModeWordWrap;
    label.highlightedTextColor = [UIColor whiteColor];
    label.numberOfLines = 0;
    label.opaque = NO; // 选中Opaque表示视图后面的任何内容都不应该绘制
    label.backgroundColor = [UIColor clearColor];
	
	//   label.text = [dict objectForKey:@"text"];
    
    if (![[dict objectForKey:@"Body"] isMemberOfClass:[NSNull class]]) {
        
         NSString *content = [dict objectForKey:@"Body"];
        content = [self changeStringFommat:content];
       label.text = content;
    }
      
    
    
    [label setFont:[UIFont systemFontOfSize:14]];
    
    [self addSubview:label];
	
    
	CGRect cellFrame = [self frame];
	cellFrame.origin = CGPointMake(0, 0);
	
	CGRect rect = CGRectInset(cellFrame, 74,  36);
	label.frame = rect;
	[label sizeToFit];
    
    cellFrame.size.height=label.frame.size.height+60;
	[self setFrame:cellFrame];
	
    self.userId=[[[NSUserDefaults standardUserDefaults] valueForKey:USER_ID] intValue];
    
	//好友名字
    NSString *friendName=@"";
    NSString *userIconImage=@"";
    
    UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(75, 6, 60, 21)];
    [fromLabel setBackgroundColor:[UIColor clearColor]];
    [fromLabel setTextColor:Text_Color];
    [fromLabel setFont:[UIFont systemFontOfSize:14]];
    [self.contentView addSubview:fromLabel];
    
    if ([[dict objectForKey:@"SenderID"] intValue] == self.userId ) {
        
        fromLabel.text = @"发给：";
        friendName = [dict objectForKey:@"Receiver"];
    
	    nameLabel.text=friendName;
        [nameLabel setFont:[UIFont boldSystemFontOfSize:18]];
        [nameLabel setTextColor:Text_Color];
	
	//好友头像
    
            if([[dict objectForKey:@"fromUserAttachmentId"] isKindOfClass:[NSNull class]]){
            userIconImage=@"";
        }
        else{
            userIconImage=[dict objectForKey:@"fromUserAttachmentId"];
        }
   
    }
    
    else{
        
        fromLabel.text = @"来自：";
        
        if([[dict objectForKey:@"Sender"] isKindOfClass:[NSNull class]]){
            friendName=@"";
        }
        else{
            friendName=[dict objectForKey:@"Sender"]; 
        }
        
    	//NSString *friendName=@"smile";
        nameLabel.text=friendName;
        [nameLabel setFont:[UIFont boldSystemFontOfSize:18]];
        [nameLabel setTextColor:Text_Color];
        
        //好友头像
        
        
        if([[dict objectForKey:@"fromUserAttachmentId"] isKindOfClass:[NSNull class]]){
            userIconImage=@"";
        }
        else{
            userIconImage=[dict objectForKey:@"fromUserAttachmentId"];
        }

        
        
    }
        
            
        CGRect frame;
        frame.size.width=50; frame.size.height=50;
        frame.origin.x=0; frame.origin.y=0;
        UIImageView* asyncImage = [[UIImageView alloc]
                                       initWithFrame:frame] ;
        asyncImage.tag = 999;
        
        //	NSString *string = [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] bundlePath],@"pic_4.jpg"];
        //	NSURL *url = [NSURL fileURLWithPath:string];
	    
        
     //   NSString *string=[NSString stringWithFormat:@"%@%@/%@/100x100",BASEURL,@"/commons/attachment/download",userIconImage];
    
      NSString *string=[NSString stringWithFormat:@"%@/%@/%@/100x100",UCENTERBASEURL,@"gravatar",userIconImage];
        
        NSURL *url = [NSURL URLWithString:string];	
        
        [asyncImage setImageWithURL:url refreshCache:NO placeholderImage:[UIImage imageNamed:@"geren_set_iphone.png"] imageType:kImageCache_Content_Icon];
        
        [asyncImage setClipsToBounds:YES];
        asyncImage.layer.cornerRadius = 3;
        [iconButton addSubview:asyncImage];
    
        // [asyncImage release];
        
	    
    
    UIImageView *arrowImageView = [[UIImageView alloc]initWithFrame:CGRectMake(280, cellFrame.size.height/2-7, 30, 30)];
    [arrowImageView setImage:[UIImage imageNamed:@"icon_jiantou_iphone.png"]];
    [self.contentView addSubview:arrowImageView];
    
    
    
    //time
    UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(74, cellFrame.size.height-25, 100, 22)];
    [timeLabel setFont:[UIFont systemFontOfSize:12]];
    [timeLabel setBackgroundColor:[UIColor clearColor]];
    [timeLabel setTextAlignment:UITextAlignmentLeft];
    [timeLabel setTextColor:Text_Color];
    [self.contentView addSubview:timeLabel];
   
    NSString *time = [dict objectForKey:@"MostRecentPostDate"];
    time = [time substringWithRange:NSMakeRange(6, 13)];
    
    long long int creatTime = [time longLongValue];
    creatTime = creatTime/1000;
    
    NSDate *nd = [NSDate dateWithTimeIntervalSince1970:creatTime];
    
    
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    
    [dateFormat setDateFormat:@"MM-dd HH:mm"];
    
    NSString *dateString = [dateFormat stringFromDate:nd];
    
    timeLabel.text = dateString;
    
    
  	    
	[label release];/////////////
}	


-(NSString *)changeStringFommat:(NSString *)str{
    
    PublicMethodEngine *methodEngine = [PublicMethodEngine sharedPublicMethodEngine];   
    
    return  [methodEngine changeStringFommat:str];
    
    
    
}



-(void)dealloc{
    
    [nameLabel release];
    [iconButton release];
    [super dealloc];
}
@end
